using CAPHandlerAttributeDemo.Api.EventHandlers;
using CAPHandlerAttributeDemo.Api.Extensions;
using DotNetCore.CAP.Internal;
using Savorboard.CAP.InMemoryMessageQueue;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddCap(x =>
{
    x.UseInMemoryStorage();
    x.UseInMemoryMessageQueue();
});
builder.Services.AddScoped<MessageEventHandler>();
var eventHandlers = typeof(IEventHandler).Assembly.GetTypes().Where(t => !t.IsInterface).ToList();
builder.Services.AddSingleton(new EventHandlerOptions(eventHandlers));
builder.Services.AddSingleton<IConsumerServiceSelector, CustomConsumerServiceSelector>(s =>
{
    var eventHandlerOptions = s.GetRequiredService<EventHandlerOptions>();
    return new CustomConsumerServiceSelector(s, eventHandlerOptions);
});

var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();